package project.web.generic;

import online.filter.FilterUtil;
import online.model.check.InputCheck;
import online.struts.action.BaseAction;
import online.struts.action.UniForm;
import project.check.range.LengthRangeCheck;
import project.svc.generic.QueryService;
import project.web.InstanceFactory;

/**
 * 汎用照会画面
 *
 * @author Tadashi Nakayama
 * @version 1.0.0
 */
public final class SearchAction extends BaseAction {

	/**
	 * 表示処理
	 *
	 * @param model 汎用データモデル
	 * @return 処理結果
	 */
	public String get(final UniForm model) {
		String result = ID_VIEW;
		if (model.hasQueryString()) {
			// 入力チェック
			checkInput(model);

			//DB情報取得
			QueryService ql = InstanceFactory.create(QueryService.class, model);

			String[] query = model.getString("QueryFile").split(",");
			for (int i = 0; i < query.length; i++) {
				ql.setQueryFile(query[i]);
				boolean ret = ql.search();
				if (!ret && i == 0) {
					result = "NOT_FOUND";
					break;
				}
			}
			model.putAll(ql.getResultModel());
		}

		return result;
	}

	/**
	 * 入力チェック
	 *
	 * @param model 汎用モデル
	 */
	private void checkInput(final UniForm model) {
		InputCheck ic = InstanceFactory.create(InputCheck.class, model);
		for (final String item : FilterUtil.toParameterKeySet(model.getQueryString())) {
			ic.add(item, new LengthRangeCheck(0, 1024));
		}
		ic.populate();
	}
}
